<?php


namespace app\api\controller;


use app\common\lib\Export;
use app\common\lib\Request;
use app\api\model\Message_prompt;
use app\api\model\Team_team;
use app\api\model\Team_user;
use think\Exception;
use app\api\model\User;
use app\api\model\Dynamic_comment;
use app\api\model\Dynamic_operation;
use app\api\model\Dynamic_dynamic;
use app\api\model\Dynamic_file;

class Prompt extends Base
{

    private $cmd_data = [
        //上传文件
        "100"=>"get_list",
    ];
    /** 入口 */
    public function index(){
        try{
            // 检验登录情况
            parent::check_user_info();

            $cmd = Request::get('cmd');
            if(!array_key_exists($cmd,$this->cmd_data)) throw new Exception("参数错误");
            $method = $this->cmd_data[$cmd];

            $this->$method();
        }catch (Exception $e){
            Export::exp_err($e->getMessage());
        }

        return '';
    }
    /** 获取消息通知列表 */
    private function get_list(){
        $data = Request::post(["page","page_size"]);

        $page = $data['page']?intval($data['page']):1;
        $page_size = $data['page_size']?intval($data['page_size']):10;
        $id = array_key_exists('id',$data)?intval($data['id']):0;

        $no_read_where = [
            "user_id"=>$this->user_id,
            "is_read"=>0,
        ];
        $list = Message_prompt::where($no_read_where)->limit($page_size)->order('create_time','desc')->select();
        $no_read_list = empty($list) ? array():$list->toArray();
        if($no_read_list){
            $no_read_ids = array_column($no_read_list,'id');
            Message_prompt::where('id','in',$no_read_ids)->data(['is_read'=>1])->update();

            // 查看是否还有未读信息
            $no_read_list = Message_prompt::where($no_read_where)->find();
            $had_no_read = $no_read_list?1:0;
        }else{
            $read_where = [
                ["user_id",'=',$this->user_id],
                ["is_read",'=',1],
            ];
            $id && $read_where[] = ['id','<',$id];
            $list = Message_prompt::where($read_where)->page($page,$page_size)->order('create_time','desc')->select();

            //无未读信息
            $had_no_read = 0;
        }
        foreach ($list as &$item){
            $operate_user = User::get($item->operate_user_id);
            $item['operate_user'] = [
                "name"=>$operate_user['name'],
                "user_pic"=> $operate_user['pic_url'],
            ];
            $item['object_data'] = $this->get_data_by_type($item->type,$item->operate_id);
        }


        $info = [
            "data"=>$list,
            "had_nod_read"=>$had_no_read,
        ];

        Export::exp_ok($info);
    }

    /** 获取消息提醒 */
    private function get_data_by_type($type,$operate_id){
        $data = [];
        switch ($type){
            //获取点赞动态信息
            case Message_prompt::$praise_dynamic_type :{
                $operation = Dynamic_operation::get($operate_id);
                if(!$operation) break;
                $dynamic = Dynamic_dynamic::get($operation->object_id);
                if(!$dynamic) break;
                $dynamic_file = Dynamic_file::where(["dynamic_id"=>$dynamic->id])->find();
                $pic = $dynamic_file?$dynamic_file->file_url:"";
                $data = [
                    "dynamic"=>[
                        "content"=>$dynamic->content,
                        'pic'=>$pic,
                    ]
                ];
                break;
            }
            //获取动态评论
            case Message_prompt::$comment_dynamic_type :{
                $comment = Dynamic_comment::get($operate_id);
                if(!$comment) break;
                $dynamic = Dynamic_dynamic::get($comment->dynamic_id);
                if(!$dynamic) break;
                $dynamic_file = Dynamic_file::where(["dynamic_id"=>$dynamic->id])->find();
                $pic = $dynamic_file?$dynamic_file->file_url:"";
                $data = [
                    "dynamic"=>[
                        "content"=>$dynamic->content,
                        'pic'=>$pic,
                    ],
                    "comment"=>$comment->content
                ];

                break;
            }
            //获取点赞评论
            case Message_prompt::$praise_comment_type :{
                $operation = Dynamic_operation::get($operate_id);
                if(!$operation) break;
                $comment = Dynamic_comment::get($operation->object_id);
                $data = [
                    "comment"=>$comment->content
                ];

                break;
            }
            //获取评论评论
            case Message_prompt::$comment_comment_type :{
                $comment = Dynamic_comment::get($operate_id);
                if(!$comment) break;
                $p_comment = Dynamic_comment::get($comment->p_id);
                if(!$p_comment) break;
                $data = [
                    "comment"=>$comment->content,
                    "p_comment"=>$p_comment->content,
                ];

                break;
            }
            //获取加入团队信息
            case Message_prompt::$join_team_type :{
                $team_user = Team_user::get($operate_id);
                if(!$team_user) break;
                $team = Team_team::get($team_user->team_id);
                if(!$team) break;

                $data = [
                    "team_name"=>$team->name,
                ];

                break;
            }

        }
        return $data;
    }
}